
<style>
.affix{top:10px;}
.head-style-list li,.editor-template-list li{cursor: pointer;}
.baomings-bulk-add {position:relative;padding: 10px;margin-bottom: 10px;border: 1px solid #dddddd;}
.item-delete{position: absolute;right:5px;top: 0px;font-size:20px;z-index:1;}
.form-control {
    background: rgb(251,251,251);
    border-radius: 3px;
}
.hidePrice .item-price{display:none !important;}
.fa-plus-circle,.fa-minus-circle{font-size:18px;margin-left:10px;cursor:pointer;}
.pickup-addresses p:nth-child(1) .fa-minus-circle{display:none;}
.pricerule-bulk-add:nth-child(1) .fa-minus-circle{display:none;}
.pricerule-bulk-add{padding-left:30px;}
.navbar-fixed-bottom {
	background-color: #f23030;
	border-top: 0;
}
</style>

{{$this->Form->create('WxActivity',array('id'=>'WxActivityAddForm','role'=>'form'))}}
		
	<div class="tab-content" style="margin-bottom:50px;">
			
		<div role="tabpanel" class="tab-pane active" id="chi-info">
			
			<div class="form-group">
				<label class="control-label" for="WxActivityName">活动名称</label>
					{{$this->Form->hidden('id')}}
					{{$this->Form->hidden('model',array('value'=>'Baoming'))}}
					{{$this->Form->hidden('status',array('default'=>1))}}					
					{{$this->Form->input('name',array('class'=>'form-control','div'=>false,'label'=>false,'required'=>'required'))}}
			</div>
			
			<div class="form-group">
				<label class="control-label" for="WxActivitySummary">活动摘要</label>
					{{$this->Form->input('summary',array('class'=>'form-control','rows'=>'6','div'=>false,'required'=>'required','label'=>false,))}}
			</div>
			
			<div class="form-group">
				<label class="control-label" for="WxStorefrontCoverImg">封面图片</label>
					{{$this->Swfupload->load('coverimg',array(
					'label'=>false,'modelClass'=>'WxActivity',
					'no_db'=>0,'upload_limit'=>1,
					'file_types'=> '*.jpg;*.gif;*.png;*.bmp',
					'isadmin'=>false
					))}}
			</div>
			<div class="form-group">
				<div class="form-inline">
					<label class="control-label" for="WxActivityHidePrice">{{$this->Form->input('hide_price',array('class'=>'form-control','default'=>1,'div'=>false,'label'=>false,))}} &nbsp;&nbsp;免费活动</label>
					<span style="margin-left:30px;color:#999;font-size:13px;">收费活动请联系客服代开微信支付账号，否则无法收款给自己。</span>
				</div>
			</div>			
		<div>
       
        <fieldset>
        <div class="clearfix">
        	<?PHP foreach($datalist as $key => $item) { 
      				$item = $item['Baoming'];
      			?>
	        <div class="baomings-bulk-add">
	        	<a class="item-delete" id="item-{{$item['id']}}" data-id="{{$item['id']}}">&times;</a>
				<div class="form-group">
					<label class="control-label">项目</label>
					{{$this->MForm->hidden('Baoming.'.$key.'.id',array('value'=> $item['id']))}}
					{{$this->MForm->input('Baoming.'.$key.'.name',array('div'=>false,'value'=> $item['name'],'required'=>'required','label'=>false));}}
				</div>
				<div class="form-group">
					<div style="width:49%;padding-right:1%;display:inline-block;">
						<label class="control-label">总数</label>
						<div class="controls">{{$this->MForm->input('Baoming.'.$key.'.storage',array('div'=>false,'type'=>'number','value'=> $item['storage'],'label'=>false));}}</div>
					</div>
					<div class="item-price" style="width:49%;display:inline-block;">
						<label class="control-label">价格</label>
						{{$this->MForm->input('Baoming.'.$key.'.price',array('div'=>false,'value'=> $item['price'],'label'=>false));}}
					</div>
				</div>
			</div>

			<?PHP } ?>
    	</div>
        </fieldset>     
		</div>
		<p style="margin-bottom: 10px;"><a  class="btn btn-default btn-sm bulk-add-new">+ 增加项目</a></p>
		<p><br/></p>		
		</div>

		

		<div class="form-group">
			<div class="form-inline">
					<label class="control-label" for="WxActivityLimited">{{$this->Form->input('limited',array('class'=>'form-control','default'=>1,'div'=>false,'label'=>false,))}} 限制总数&nbsp;</label>
					<span style="margin-left:30px;color:#999;font-size:13px;">限制数量不能超出总数。</span>
				</div>
		</div>

		<div class="form-group">
			<div class="form-inline">
				<label class="control-label" for="WxActivityAllowExpress">{{$this->Form->input('allow_express',array('type'=>'checkbox','class'=>'form-control','default'=>1,'div'=>false,'label'=>false,))}} 快递</label>
				<p style="margin-left:30px;color:#999;font-size:13px;">{{$this->Form->input('ship_fee',array('class'=>'form-control','div'=>false,'label'=>false,'style'=>'width: 150px;','placeholder'=>'快递费，默认为0'))}}
				&nbsp;&nbsp;&nbsp;	满 {{$this->Form->input('free_express',array('class'=>'form-control','div'=>false,'style'=>'width:80px;','label'=>false,'placeholder'=>'总金额'))}} 免邮
			</p>
			</div>
		</div>
		<div class="form-group">
				<div class="form-inline">
					<label class="control-label" for="WxActivityAllowPickup">{{$this->Form->input('allow_pickup',array('type'=>'checkbox','class'=>'form-control','default'=>0,'div'=>false,'label'=>false,))}} 自提</label>
					<div class="pickup-addresses">
					<?PHP foreach($this->data['WxActivity']['pickup_address'] as $key => $address) {

					?>
					
					<p style="margin-left:30px;margin-bottom:5px;color:#999;font-size:13px;">{{$this->Form->input('WxActivity.pickup_address.'.$key,array('class'=>'form-control','value'=>$address,'div'=>false,'label'=>false,'placeholder'=>'输入自提地址'))}}
						<i class="fa fa-plus-circle"></i> &nbsp; <i class="fa fa-minus-circle"></i>
					</p>
					<?PHP } ?>
					</div>
				</div>
			</div>

		<hr/>
		<p><a href="javascript:void(0)" onclick="$('#more-property').toggle();">▽ 更多高级设置 ▽</a></p>

		<div id="more-property" style="display:none;">
			
			<div class="form-group">
				<label class="control-label" for="WxActivityButtonName">按钮文字</label>
				{{$this->Form->input('button_name',array('class'=>'form-control','placeholder'=>'如“我要报名”，“我要下单”等','div'=>false,'label'=>false))}}
			</div> 
			<!--<div class="form-group">
				<label class="control-label" for="WxActivitySummary">项目规格1</label>
					{{$this->Form->input('property1',array('class'=>'form-control','rows'=>'3','div'=>false,'label'=>false,'placeholder'=>'如颜色、尺码等，一行一个'))}}
				<p><small>配置规格后，每个活动项都可以选择规则。不同规格的价格相同，价格不同的不要使用规格实现。</small></p>
			</div>
			<div class="form-group">
				<label class="control-label" for="WxActivitySummary">项目规格2</label>
					{{$this->Form->input('property2',array('class'=>'form-control','rows'=>'3','div'=>false,'label'=>false,'placeholder'=>'如颜色、尺码等，一行一个'))}}

			</div>-->

			<div class="form-group">
				<div class="form-inline">
					<label class="control-label">扩展字段</label>
					<div class="serialize_fields">
						<?PHP foreach($this->data['WxActivity']['serialize_info'] as $key => $info) {

						?>

						<p style="margin-left:30px;margin-bottom:5px;color:#999;font-size:13px;">

							{{$this->Form->input('WxActivity.serialize_info.'.$key.'.type',array('class'=>'form-control serialize-type','value'=>$info['type'],'div'=>false,'type'=>'select','options'=>array(''=>'选择类型','idcard'=>'身份证','passport'=>'护照','email'=>'邮箱','text'=>'文字','date'=>'日期','number'=>'数字'),'label'=>false,))}}&nbsp;
							{{$this->Form->input('WxActivity.serialize_info.'.$key.'.name',array('class'=>'form-control serialize-name','style'=>'width: 120px;','value'=>$info['name'],'div'=>false,'label'=>false,'placeholder'=>'输入名称'))}}

							<i class="fa fa-plus-circle"></i> &nbsp; <i class="fa fa-minus-circle"></i>
						</p>
						<?PHP } ?>
					</div>
				</div>
			</div>

			<div id="price-rules">
				<p>促销折扣</p>
				<?PHP foreach($price_rules as $key => $rule) { 
	      				
	      			?>
		        <div class="pricerule-bulk-add">		        	
					<div class="form-group  form-inline">
						{{$this->MForm->input('WxActivity.price_rules.'.$key.'.type',array('div'=>false,'type'=> 'select','options'=>array('discount'=>'折扣'),'value'=> $rule['type'],'required'=>'required','label'=>false));}}

						{{$this->MForm->input('WxActivity.price_rules.'.$key.'.num',array('div'=>false,'type'=>'number','value'=> $rule['num'],'style'=>'width:45px;margin:0 5px;text-align: center;','label'=>false));}}
							
						{{$this->MForm->input('WxActivity.price_rules.'.$key.'.val',array('div'=>false,'type'=>'number','value'=> $rule['val'],'style'=>'width:80px;','label'=>false));}}
						
						<i class="fa fa-plus-circle"></i> &nbsp; <i class="fa fa-minus-circle"></i>
					</div>
				</div>
				<?PHP } ?>

				<p><small>折扣，满几件打几折，折扣使用小数，如85折填0.85。最多折扣的填最前面。</small></p>
			</div>
		</div>
		<p><br/></p>
		<!--
		<div class="form-group">
			<label class="col-xs-3 control-label" for="WxActivityAddress">有效时间</label>
			<div class="col-xs-9 controls form-inline">
				{{$this->Form->input('expired',array('class'=>'form-control','default'=>0,'div'=>false,'label'=>false,))}}
				<p>按开始结束时间生效</p>
			</div>
		</div>
						
			<div class="form-group">
				<label class="col-xs-3 control-label" for="WxStorefrontPriority">开始时间</label>
				<div class="col-xs-9 controls form-inline">
					{{$this->Form->input('started',array('class'=>'form-control','type'=>'datetime','dateFormat'=>'YMD','timeFormat'=>24,
					'monthNames'=> array(
						1=>'1月',2=>'2月',3=>'3月',4=>'4月',5=>'5月',6=>'6月',
						7=>'7月',8=>'8月',9=>'9月',10=>'10月',11=>'11月',12=>'12月',
					),
					'div'=>false,'label'=>false,))}}
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 control-label" for="WxStorefrontPriority">结束时间</label>
				<div class="col-xs-9 controls form-inline">
					{{$this->Form->input('ended',array('class'=>'form-control','type'=>'datetime','dateFormat'=>'YMD','timeFormat'=>24,
					'monthNames'=> array(
						1=>'1月',2=>'2月',3=>'3月',4=>'4月',5=>'5月',6=>'6月',
						7=>'7月',8=>'8月',9=>'9月',10=>'10月',11=>'11月',12=>'12月',
					),
					'div'=>false,'label'=>false,))}}
				</div>
			</div> -->
	
	
		<div class="form-group">
			<button class="btn btn-primary btn-lg {{if $is_mobile}}btn-block navbar-fixed-bottom{{/if}}" type="submit">
				{{if $current_action=='add'}}完成创建{{else}}保存修改{{/if}}
			</button>
		</div>
	</div>
{{$this->Form->end()}}

<script>

$(function(){

	$('#WxActivityHidePrice').click(function(){
		if(this.checked){
			$('#WxActivityAddForm').addClass('hidePrice');
			$('#WxActivityAddForm .item-price :input').each(function(){
				$(this).data('val',this.value);
				this.value = 0;
			})
		}
		else{
			$('#WxActivityAddForm').removeClass('hidePrice');
			$('#WxActivityAddForm .item-price :input').each(function(){
				$(this).val($(this).data('val'));
			})
		}	
	})
	
	$('#WxActivityAddForm').data('submitCallback',function(request){
		if(request.ret == 0 && request.data.WxActivity.id > 0) {
			showSuccessMessage('活动已成功{{if $current_action=='add'}}创建{{else}}修改{{/if}}，即将进入详情页');
			window.location = '/wx_activities/view/' + request.data.WxActivity.id ;
		}
	});

	$(document).on('click','#price-rules .fa-plus-circle',function(){
		var clone = $(this).parents('.pricerule-bulk-add:first').clone();
        clone.find('input').val('');
        clone.insertAfter( $(this).parents('.pricerule-bulk-add:first') );
        
        $('.pricerule-bulk-add').each(function(i){
        	$(this).find(':input').each(function(){
	            var name = $(this).attr('name');
	            //alert(name.replace(/\[\d*\]/,'[10]'));
	            $(this).attr('name',name.replace(/\[\d*\]/,'['+i+']'));
	        })
        });
	});
	$(document).on('click','.pricerule-bulk-add .fa-minus-circle',function(){
	    if( $('.pricerule-bulk-add').size() > 1 ) {
            $(this).parents('.pricerule-bulk-add:first').remove();
		}
		else{
            $(this).parents('.pricerule-bulk-add:first').find(':input').val('');
		}
    });


    $(document).on('click','.serialize_fields .fa-plus-circle',function(){
        var clone = $(this).parents('p:first').clone();
        clone.find('input').val('');
        clone.insertAfter( $(this).parents('p:first') );

        $('.serialize_fields p').each(function(i){
            $(this).find(':input').each(function(){
                var name = $(this).attr('name');
                //alert(name.replace(/\[\d*\]/,'[10]'));
                $(this).attr('name',name.replace(/\[\d*\]/,'['+i+']'));
            });
		});

    });
    $(document).on('click','.serialize_fields .fa-minus-circle',function(){
        if( $('.serialize_fields p').size() > 1) {
            $(this).parents('p:first').remove();
		}
        else{
            $(this).parents('p:first').find(':input').val('');
        }
    });

	$(document).on('click','.pickup-addresses .fa-plus-circle',function(){
	    var clone = $(this).parents('p:first').clone();
        clone.find('input').val('');
        clone.insertAfter( $(this).parents('p:first') );
        
        $('.pickup-addresses').find(':input').each(function(i){
            var name = $(this).attr('name');
            //alert(name.replace(/\[\d*\]/,'[10]'));
            $(this).attr('name',name.replace(/\[\d*\]/,'['+i+']'));
        });
        
	});
    $(document).on('click','.pickup-addresses .fa-minus-circle',function(){
        if( $('.pickup-addresses p').size() > 1) {
            $(this).parents('p:first').remove();
        }
        else{
            $(this).parents('p:first').find(':input').val('');
        }
    });

    $(document).on('change', '.serialize-type', function() {
        var txt = $('option:selected',this).text();
        if($.inArray(txt,['身份证','护照','邮箱','日期']) >= 0 ) {
            $(this).siblings('.serialize-name').val( txt );
        }
        else{
            var txt = $(this).siblings('.serialize-name').val( );
            if($.inArray(txt,['身份证','护照','邮箱','日期']) >= 0 ) {
                $(this).siblings('.serialize-name').val('');
            }
		}
    });
	
	$(document).on('click','.bulk-add-new',function(){
		var obj = $('.baomings-bulk-add:last');

		var newobj = obj.clone();newobj.find(':input').val('');
		newobj.insertAfter(obj);
		
		$('.baomings-bulk-add').each(function(i){
			$(this).find(':input').each(function(){
				var name = $(this).attr('name');
				//alert(name.replace(/\[\d*\]/,'[10]'));
				$(this).attr('name',name.replace(/\[\d*\]/,'['+i+']'));
			});
		});		
	})
	$(document).on('click','.item-delete',function(){
		if( $('.baomings-bulk-add').size() > 1 ){
			var id = $(this).data('id');
			if( id > 0 ) {
				if(confirm('请注意删除已添加项，可能影响已提交的数据，是否确认删除？')) {
					ajaxAction('/baomings/delete/'+id,null,null,function(request){
						if(request.ret == 0) {
							showSuccessMessage(request.msg);
							$('#item-'+id).remove();
						}
						else{
							showErrorMessage(request.msg);
						}
						

					});
				}
			}
			else{
				$(this).parents('.baomings-bulk-add').remove();	
			}
		}
		else{
			alert('项目至少要保留1个');		
		}		
	})
})
</script>